package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String v = req.getParameter("q");
        if ("select".equals(v)){
            select(req, resp);
        }else if ("delete".equals(v)){
            delete(req, resp);
        }else if ("insert".equals(v)){
            insert(req, resp);
        }else if ("update".equals(v)){
            update(req, resp);
        }else if ("jump".equals(v)){
            jump(req, resp);
        }else if ("jumpp".equals(v)){
            jumpp(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        doGet(req,resp);
    }
    protected void jump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        req.getRequestDispatcher("/AddManager.jsp").forward(req,resp);
    }
    protected void jumpp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        GuestbookService guestbookService = new GuestbookServiceImpl();
        Guestbook guestbook = guestbookService.query(Integer.parseInt(id));
        req.setAttribute("guest",guestbook);
//        resp.sendRedirect(req.getContextPath()+"/Update.jsp");
        req.getRequestDispatcher("/Update.jsp").forward(req,resp);
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService guestbookService = new GuestbookServiceImpl();
        List<Object> list = guestbookService.allquery();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html;charset=utf-8");
       req.setCharacterEncoding("utf-8");
       String name = req.getParameter("name");
       String phone = req.getParameter("phone");
       String email = req.getParameter("email");
       String title = req.getParameter("title");
       String content = req.getParameter("content");
       Date a = new Date();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       String createdtime = sdf.format(a);
        Guestbook guestbook = new Guestbook();
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(email);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setCreatedtime(createdtime);
        GuestbookService guestbookService = new GuestbookServiceImpl();
        int i = guestbookService.add(guestbook);
        if (i==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
        }
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String createdtime = req.getParameter("createdtime");
        Guestbook guestbook = new Guestbook();
        guestbook.setId(Integer.parseInt(id));
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(email);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setCreatedtime(createdtime);
        GuestbookService guestbookService = new GuestbookServiceImpl();
        int a = guestbookService.update(guestbook);
        if (a==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
//            resp.sendRedirect(req.getContextPath()+"/m?q=select");
        }

    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService guestbookService = new GuestbookServiceImpl();
        String o = req.getParameter("id");
        int id = Integer.parseInt(o);
        int i = guestbookService.delete(id);
        if (i==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
        }
    }

}
